//package com.xiao.message.service.handler;
//
//import com.rabbitmq.client.Channel;
//import com.xiao.client.blade.feign.BladeUserClient;
//import com.xiao.client.customer.dto.CustomerDTO;
//import com.xiao.client.customer.feign.CustomerClient;
//import com.xiao.client.order.dto.OrderDto;
//import com.xiao.client.order.dto.OrderPackageDto;
//import com.xiao.client.order.feign.OrderClient;
//import com.xiao.client.order.feign.OrderPackageClient;
//import com.xiao.message.service.NotificationService;
//import com.xiao.message.service.dto.NotificationDto;
//import com.xiao.message.service.dto.ShipNoticeDto;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.ExchangeTypes;
//import org.springframework.amqp.rabbit.annotation.Exchange;
//import org.springframework.amqp.rabbit.annotation.Queue;
//import org.springframework.amqp.rabbit.annotation.QueueBinding;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.amqp.support.AmqpHeaders;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.messaging.handler.annotation.Header;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//
//import java.time.LocalDateTime;
//import java.time.format.DateTimeFormatter;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Objects;
//
///**
// * @author mingyuezheng
// * 接收发货消息
// */
//
//@Slf4j
//@Component
//public class DeliveryNoticeHandler {
//    private final BladeUserClient bladeUserClient;
//    private final OrderClient orderClient;
//    private final OrderPackageClient orderPackageClient;
//    private final CustomerClient customerClient;
//    private final NotificationService notificationService;
//
//    @Value("${wechat.subscribeMessage.ship-template-id}")
//    private String templateId;
//    @Value("${wechat.subscribeMessage.ship-page}")
//    private String pageUrl;
//
//    public DeliveryNoticeHandler(BladeUserClient bladeUserClient,
//                                 OrderClient orderClient,
//                                 CustomerClient customerClient,
//                                 OrderPackageClient orderPackageClient,
//                                 NotificationService notificationService) {
//        this.bladeUserClient = bladeUserClient;
//        this.orderClient = orderClient;
//        this.customerClient = customerClient;
//        this.orderPackageClient = orderPackageClient;
//        this.notificationService = notificationService;
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "queue.order.ship.subscribe.notice", durable = "true", autoDelete = "false"),
//            exchange = @Exchange(value = "exchange.fanout.order.ship.subscribe", type = ExchangeTypes.FANOUT)), ackMode = "MANUAL")
//    public void shipSubscribeNotice(ShipNoticeDto dto,
//                                    @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag,
//                                    Channel channel) throws Exception {
//        log.info("接收到发货消息：{}", dto);
////        R<UserPackageDto> packageDto = orderClient.getByPackageId(dto.getPackageId());
////        UserPackageDto data = packageDto.getData();
////
////        if (ObjectUtil.isEmpty(data)) {
////            log.error("获取包裹信息失败！packageId:{},{}", dto.getPackageId(), packageDto);
////            throw new RuntimeException("获取包裹信息失败！");
////        }
//        final OrderPackageDto orderPackageDto = orderPackageClient.findById(dto.getPackageId());
//        if (Objects.isNull(orderPackageDto)) {
//            log.error("获取包裹信息失败！orderPackageId:{}", dto.getPackageId());
//            throw new Exception("获取包裹信息失败,未查到订单包裹相关信息");
//        }
//
//        final OrderDto orderDto = orderClient.findById(dto.getOrderId());
//        final Long customerId = orderDto.getBuyerId();
//        final CustomerDTO customerDTO = customerClient.findById(customerId);
//        if (Objects.isNull(customerDTO) && !StringUtils.hasText(customerDTO.getAccount())) {
//            log.error("用户账户信息失败！customer:{},响应{}", customerDTO);
//            throw new RuntimeException("获取包裹信息失败！");
//        }
//
//        Map<String, Object> wechatArgsMap = new HashMap<>(4);
//        HashMap<String, String> thing14 = new HashMap<>(1);
//        thing14.put("value", orderPackageDto.getExpressName());
//        wechatArgsMap.put("thing14", thing14);
//
//        HashMap<String, String> thing13 = new HashMap<>(1);
//        thing13.put("value", dto.getLogisticCode());
//        wechatArgsMap.put("character_string13", thing13);
//
//        HashMap<String, Object> date4 = new HashMap<>(1);
//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
//        LocalDateTime expressTime = orderPackageDto.getExpressTime();
//        date4.put("value", dateTimeFormatter.format(expressTime));
//        wechatArgsMap.put("date4", date4);
//
//        HashMap<String, Long> string15 = new HashMap<>(1);
//        string15.put("value", dto.getOrderId());
//        wechatArgsMap.put("character_string15", string15);
//
//        NotificationDto notificationDto = new NotificationDto();
//        notificationDto.setTargetType("ORDER");
//        notificationDto.setTopic("ORDER");
//        notificationDto.setType("REMIND");
//        notificationDto.setContent("您的订单：" + dto.getOrderId() + "已经发货啦～");
//        notificationDto.setTitle("发货提醒");
//        notificationDto.setMobile(customerDTO.getAccount());
//        notificationDto.setUserId(String.valueOf(customerDTO.getUserId()));
//        notificationDto.setTemplateId(templateId);
//        notificationDto.setWechatPageUrl(pageUrl + dto.getOrderId());
//        notificationDto.setWechatTemplateArgs(wechatArgsMap);
//        log.debug("推送到fanout交换机的发货消息：{}", notificationDto);
//        notificationService.createChannelNotificationToUser(String.valueOf(customerDTO.getUserId()), notificationDto);
//
//        channel.basicAck(deliveryTag, false);
//    }
//
//}
